<template>
  <quill-editor
      ref="myLQuillEditor"
      v-model="content"
      :options="readonlyEditorOption"
      class="readoletEditor"
      @blur="onEditorBlur"
      @focus="onEditorFocus"
      @change="onEditorChange">
    >
  </quill-editor>
</template>

<script>
// 引入样式和quillEditor
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import {quillEditor} from 'vue-quill-editor'
// 工具栏配置项
const toolbarOptions = [
  // 加粗 斜体 下划线 删除线 -----['bold', 'italic', 'underline', 'strike']
  ['bold', 'italic', 'underline', 'strike'],
  // 引用  代码块-----['blockquote', 'code-block']
  ['blockquote', 'code-block'],
  // 1、2 级标题-----[{ header: 1 }, { header: 2 }]
  [{header: 1}, {header: 2}],
  // 有序、无序列表-----[{ list: 'ordered' }, { list: 'bullet' }]
  [{list: 'ordered'}, {list: 'bullet'}],
  // 上标/下标-----[{ script: 'sub' }, { script: 'super' }]
  [{script: 'sub'}, {script: 'super'}],
  // 缩进-----[{ indent: '-1' }, { indent: '+1' }]
  [{indent: '-1'}, {indent: '+1'}],
  // 文本方向-----[{'direction': 'rtl'}]
  [{direction: 'rtl'}],
  // 字体大小-----[{ size: ['small', false, 'large', 'huge'] }]
  [{size: ['small', false, 'large', 'huge']}],
  // 标题-----[{ header: [1, 2, 3, 4, 5, 6, false] }]
  [{header: [1, 2, 3, 4, 5, 6, false]}],
  // 字体颜色、字体背景颜色-----[{ color: [] }, { background: [] }]
  [{color: []}, {background: []}],
  // 字体种类-----[{ font: [] }]
  [{font: []}],
  // 对齐方式-----[{ align: [] }]
  [{align: []}],
  // 清除文本格式-----['clean']
  ['clean'],
  // 链接、图片、视频-----['link', 'image', 'video']
  ['image', 'video']
]
export default {
  name: "Editor",
  props: {
    value: {
      type: String,
      default: ''
    },
    isToolbar:{
      type:Boolean,
      default:true
    },
    disabled:{  //是否禁用编辑
      type:Boolean,
      default:false
    }
  },
  components: {
    quillEditor
  },
  data() {
    return {
      content:this.value
    }
  },
  computed:{
    readonlyEditorOption(){
      return {
        modules: {
          toolbar: this.isToolbar?toolbarOptions:null,
        },
        placeholder: '请输入内容',
        readOnly: true,
        theme: 'snow'  // or 'bubble'
      }
    }
  },
  methods:{
    onEditorBlur(quill) {
      console.log('editor blur!', quill)
    },
    onEditorFocus(quill) {
     if (this.disabled) {
       quill.enable(false)
     }
     console.log('editor focus!', quill)
    },
    onEditorChange({quill, html, text}) {
      console.log('editor change!', quill, html, text)
      this.$emit('update:value', html)
    },
  },
  watch:{
    value(val){
      this.content=val
    }
  }
}
</script>

<style scoped lang="less">
.readoletEditor {
  height: 100%;
}
</style>